package com.scqkxx.gamebox.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jakewharton.rxbinding2.view.RxView;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.activities.utils.ActivityUtil;
import com.scqkxx.gamebox.activity.myself.LoginActivity;
import com.scqkxx.gamebox.activity.news.NewsActivity;
import com.scqkxx.gamebox.activity.webview.web.WebViewActivity;
import com.scqkxx.gamebox.activity.webview.web.model.WebViewConfig;
import com.scqkzqtz.base.library.utils.DrawableUtil;
import com.scqkzqtz.base.library.utils.PreferenceUtils;
import com.scqkzqtz.base.model.DBOperationSite;
import com.scqkzqtz.base.model.OperationSiteEntity;
import com.scqkzqtz.base.utils.OperationSiteUtils;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import io.reactivex.android.schedulers.AndroidSchedulers;


/**
 * Created by zsx on 2018/2/1.
 */

public class StartActivity extends AppCompatActivity {
    private TextView start_next;
    private ImageView im_bp;
    private String WebUrl = "";
    private String SYBT_Title = "";
    private boolean isFirstRun;//判断进入页面标志,true进入主页，false进入导航页

    int i = 5;
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            // UI thread
            runOnUiThread(() -> {
                start_next.setText(i + "跳过");
                if (i <= 1) {
                    task.cancel();
                    startToMain();
                }

                if (i == 2 || i == 4) { //请求两次运营位数据，防止网络差情况未请求到
                    OperationSiteUtils.getOperationSite();
                }
                i--;
            });
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.0 全透明实现
            //getWindow.setStatusBarColor(Color.TRANSPARENT);
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //4.4 全透明状态栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            Window window = getWindow();
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        init();
    }

    private void init() {
        isFirstRun = PreferenceUtils.getBoolean("isFirstRun", false);
        if (!isFirstRun) {
            startToWelcome();
        } else {
            initView();
        }
    }

    @SuppressLint("CheckResult")
    private void initView() {
        start_next = findViewById(R.id.start_next);
        start_next.setBackground(DrawableUtil.getSolidRectDrawable(50, Color.BLACK));
        start_next.getBackground().setAlpha(125);
        im_bp = findViewById(R.id.im_bp);

        RxView.clicks(start_next)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .delay(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    task.cancel();
                    startToMain();
                });

        RxView.clicks(im_bp)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .delay(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    if (TextUtils.isEmpty(WebUrl)) {
                        return;
                    }
                    task.cancel(); // 取消
                    startToMain();
                    startActivity(WebViewActivity.getIntent(this, new WebViewConfig(WebUrl)));
                });
        OperationSiteUtils.queryOperationSite("SY_BP", (list, images) -> {
            if (list != null && list.size() > 0) {
                DBOperationSite operationSite = list.get(0).getDbOperationSite();
                WebUrl = operationSite.getUrl();
                SYBT_Title = operationSite.getTitle();
                Glide.with(StartActivity.this).load(operationSite.getThumbnail()).into(im_bp);
                im_bp.setVisibility(View.VISIBLE);
                Timer timer = new Timer();
                timer.schedule(task, 0, 1000);
            } else {
                startToMain();
            }
        });
    }

    /**
     * 跳转到主界面
     */
    private void startToMain() {
       boolean isLogin =  PreferenceUtils.getBoolean("isLogin" , false);
       if (isLogin){
           ActivityUtil.switchTo(this , NewsActivity.class);
       }else{
           ActivityUtil.switchTo(this , LoginActivity.class);
       }
        finish();
    }

    /**
     * 跳转到导航页
     */
    private void startToWelcome() {
        PreferenceUtils.commitBoolean("isFirstRun", true);
        Intent intent = new Intent(this, WelComeActivity.class);
        startActivity(intent);
        finish();
    }

}
